//
//  LFVisitView.swift
//  SDAutoWeibo
//
//  Created by 刘健 on 16/3/21.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit


  //MARK : --  代理声明
   //需要 继承 NSObjectProtocol
protocol LFVisitViewDelegate : NSObjectProtocol {
    
    //点击注册
    func didClickRegister()
    
    //点击登录
    func didClickLogin()
    
}

class LFVisitView: UIView {
    
    //声明代理属性
  weak var delegate :LFVisitViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
          super.init(frame: frame)
            
        addSubview(iconView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        addSubview(coverView)
        
        //更改背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        prepareLayout()
        
    }
    
   private func prepareLayout() {
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-40)
        }
        
        homeView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-44)
        }
        //
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_centerY).offset(44)
            make.centerX.equalTo(self)
            make.width.equalTo(200)
        }
        //
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel)
            make.width.equalTo(80)
            make.height.equalTo(35)
        }
        //
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerBtn)
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.width.equalTo(80)
            make.height.equalTo(registerBtn)
        }
        
        coverView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
            make.height.equalTo(250)
            make.width.equalTo(self)
        }


        
    }
    
   
//    //会调用2次 如果后期修改布局 UI可能会有异常
    override func layoutSubviews() {
        super.layoutSubviews()

           }
    
    //MARK : -- 切换控制器，更变图片和label
    func changeImageAndLabel(imageName:String, labelText:String){
        
        homeView.hidden = true
        
//        coverView.hidden = true 或者
        sendSubviewToBack(coverView)
        
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = labelText
        
    }
    
      //MARK : -- iconView滚动效果 --核心动画
    func iconViewTurnAround() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //旋转角度
        animation.toValue = M_PI * 2
        
        //旋转时间
        animation.duration = 30
        
        //旋转次数
        animation.repeatCount = MAXFLOAT
        
        //不停止旋转
        animation.removedOnCompletion = false
        
        //添加动画
        iconView.layer.addAnimation(animation, forKey: nil)
        
    }
    
      //MARK : -- 按钮的点击事件
    func didClickRegisterBtn() {
        //响应代理
        delegate?.didClickRegister()
        
    }
    
    func didClickLoginBtn() {
        //响应代理
        delegate?.didClickLogin()
    }
    
    
    //MARK : -- 懒加载
    //滚轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    //主图标
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //说明label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.text = "关注一些人,看看有什么惊喜！！"
        label.font = UIFont.systemFontOfSize(17);
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.blackColor()
        label.numberOfLines = 0
      
        
        return label
    }()
    //注册btn
    private lazy var registerBtn: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "didClickRegisterBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
//        UILayoutGuide
        
        return button
    }()
    //登录btn
    private lazy var loginBtn: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        button.addTarget(self , action: "didClickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    //遮层View
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
   
}
